#include "custombox.h"
#include <QPainter>
#include <QCloseEvent>
#include "ntlauncher.h"
#include <QDesktopWidget>
#include "funcmgr.h"

CustomBox::CustomBox(QWidget *parent) : QWidget(parent)
    , EventLoop(Q_NULLPTR)
    , Result(ID_CANCEL)
{
    ui.setupUi(this);

    //设置标题
    setWindowTitle(QStringLiteral("温馨提示"));

    //设置窗口去除边框
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    //设置窗口背景透明
    this->setAttribute(Qt::WA_TranslucentBackground);

    //初始化标题栏
    AtTitleBar = new TitleBar(this);
    AtTitleBar->move(0, 0);
    AtTitleBar->SetTitleWidth(this->width());

    //信号
    QObject::connect(ui.pButtonOk, SIGNAL(clicked()), this, SLOT(OnOkClicked()));
    QObject::connect(ui.pButtonCancel, SIGNAL(clicked()), this, SLOT(OnCancelClicked()));
    QObject::connect(AtTitleBar, SIGNAL(sButtonCloseClicked()), this, SLOT(OnBoxCloseClicked()));
}

CustomBox::~CustomBox() {	}

void CustomBox::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    //绘制窗口背景色
    QPainterPath pathBack;
    pathBack.setFillRule(Qt::WindingFill);
    pathBack.addRect(QRect(0, 0, this->width(), this->height()));
    painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
    painter.fillPath(pathBack, QBrush(QColor(25, 30, 40)));

    //绘制窗口边框
    QPen pen(QColor(100, 100, 100));
    painter.setPen(pen);
    painter.drawRect(0, 0, this->width() - 1, this->height() - 1);

    return QWidget::paintEvent(event);
}

void CustomBox::SetContentText(QString ContentText)
{
    ui.MessageContent->setText(ContentText);
}

void CustomBox::SetButtonType(BType ButtonType)
{
    switch (ButtonType)
    {
    case BUTTON_OK:
        ui.pButtonCancel->setVisible(false);
        break;
    default:
        break;
    }
}

ChosseResult CustomBox::BoxShow()
{
    setWindowModality(Qt::ApplicationModal);
    show();

    EventLoop = new QEventLoop(this);
    EventLoop->exec();

    return Result;
}

void CustomBox::OnOkClicked()
{
    Result = ID_OK;
    close();
}

void CustomBox::OnCancelClicked()
{
    Result = ID_CANCEL;
    close();
}

void CustomBox::OnBoxCloseClicked()
{
    Result = ID_CANCEL;
    close();
}

void CustomBox::closeEvent(QCloseEvent *event)
{
    if (EventLoop != Q_NULLPTR)
        EventLoop->exit();

    event->accept();
}
